$(document).ready(function() {

	$("#register_btn").click(function(evt) {
		evt.preventDefault();
		var vali = true;
		$("#message-form .txt").each(function() {
			if (vali) {
				if ($(this).val() == "" || $(this).parent().next().html() != "")
					vali = false;
			}
		});
		if (vali)
			$("#message-form").submit();
	});
	
	$(".zebra tr:even").addClass("alt");
	$(".zebra tr").mouseover(function() {
		$(this).addClass("over");
	}).mouseout(function() {
		$(this).removeClass("over");
	});
	
	$("#select-all").change(function() {
		if ($(this).attr("checked") == "checked") {
			$(".message-index").attr("checked", "checked");
		}
		else {
			$(".message-index").removeAttr("checked");
		}
	});
	
	$("#delete-message").click(function(evt) {
		evt.preventDefault();
		var ids = "";
		$.each($(".message-index:checked"), function(index, item) {
			ids += "," + item.value;
		});
		if (ids != "") {
			ids = ids.substr(1);
			$.ajax({
				url : "deleteMessage.do?ids=" + ids,
				dataType : 'json',
				success : function(data) {
					var msg = data['error'];
					if (msg == "") {
						$(".message-index:checked").parent().parent().hide(600, function() {
							if ($(".message-index:visible").length == 0) {
								$(".zebra").append($("<tr><td style='text-align:center;' colspan='4'>No messages available...</td></tr>"));
							}
						});
						
					}
					else {
						alert(msg);
					}
				},
				error : function(xhr, testStatus, error) {
					alert("Communication error!");
					return;
				}
			});
		}
		else {
			alert(ids);
		}
	});
	
	var vcontent = function() {
		if ($("#content").val() == "") {
			$("#content").parent().next().html(getMessage("error", "Content cannot be empty!"));
			return false;
		}
		else {
			$("#content").parent().next().html("");
			return true;
		}
	};
	
	var vtitle = function() {
		if ($("#title").val() == "") {
			$("#title").parent().next().html(getMessage("error", "Title cannot be empty!"));
			return false;
		}
		else {
			$("#title").parent().next().html("");
			return true;
		}
	};
	
	var vreceiver = function() {
		//var re = /^[\w\d._-]+@[\w\d.-]+\.[\w]{2,4}$/i;
		if ($("#receiver").val() == "") {
			$("#receiver").parent().next().html(getMessage("error", "Receiver cannot be empty!"));
			return false;
		}
		/*
		else if (!$("#receiver").val().match(re)) {
			$("#receiver").parent().next().html(getMessage("error", "Invalid email format!"));
			return false;
		}
		*/
		else {
			$("#receiver").parent().next().html("");
			return true;
		}
	};
	
	
	$("#vcontent").focusout(vcontent);
	
	$("#title").focusout(vtitle);
	
	$("#receiver").focusout(vreceiver);
	
	$("#receiver").autocomplete(
			{
				source : function(request, response) {
					$.ajax({
						url : "messageAutoComplete.do",
						dataType : "json",
						data : {
							prefix : request.term
						},
						success : function(data) {
							response($.map(data, function(item) {
								return {
									label : item.name,
									value : item.name,
									email : item.email,
									thumb : item.thumb
								}
							}));
						}
					});
				},
				select : function(event, ui) {
					$("#hiden-receiver").val(ui.item.email);
				}
				/*
				, 
				focus : function(event, ui) {
				},
				
				
				close : function(event, ui) {
				}
				*/
			}).data("autocomplete")._renderItem = function(ul, item) {
				return $("<li></li>").data("item.autocomplete", item).append("<a class='name-list'><img class='smallthumb' src='" + item.thumb + "'/>" + item.label + "</a>").appendTo(ul);
		};
});

function getMessage(sign, message) {
	return "<div class='tip_" + sign + "'><span class='icon_" + sign + "'></span>" + message + "</div>";
}